package Listener.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * 被观察者 计算器
 */
public class CalculatorObserver {

        private double result;
        private List<Observer> observers = new ArrayList<>();

        public void addObserver(Observer observer) {
            observers.add(observer);
        }

        public void removeObserver(Observer observer) {
            observers.remove(observer);
        }

        public void setResult(double result) {
            this.result = result;
            notifyObservers();
        }

        private void notifyObservers() {
            for (Observer observer : observers) {
                observer.update(result);
            }
        }
    }

